<?php

class PostController extends Blog_Controller_AdminController {

	public function indexAction() {
		$this->forward('list');
	}

	/**
	 * 
	 * @return \Zend_Form
	 */
	private function renderForm($name) {
		$PostConfig = require APPLICATION_PATH . '/configs/post.config.php';
		$Form = new Zend_Form(array('name' => $name));
		//$n = new ZendX_JQuery_Form_Element_AutoComplete('Tags', array('label' => 'Từ khóa'));
		$Published = new ZendX_JQuery_Form_Element_DatePicker('Published', array('label' => 'Ngày đăng', 'value' => Zend_Date::now()->toString('dd-MM-Y H:m:s')));
		$Published->setJQueryParam('onSelect', '');
		$Form->setMethod(Zend_Form::METHOD_POST)
				->setDecorators(array(array('ViewScript', array('viewScript' => '/post/_postForm.phtml',
					'plugins' => implode(',', $PostConfig['editor']['plugins']), 'toolbars' => $PostConfig['editor']['toolbars']))))
				->addElements(array(
					new Zend_Form_Element_Text('Title', array('placeholder' => 'Tiêu đề', 'class' => 'input big-input', 'required' => TRUE, 'filters' => array('StringTrim'))),
					new Zend_Form_Element_Textarea('Content', array('label' => 'Nội dung', 'class' => 'tiny-mce', 'style' => 'width: 95%;', 'required' => TRUE, 'filters' => array('StringTrim'))),
					$Published,
					new Zend_Form_Element_Checkbox('Status', array('label' => 'Hiển thị', 'value' => '1')),
					new Zend_Form_Element_Text('Tags', array('label' => 'Từ khóa')),
					new Zend_Form_Element_Submit('submit', 'Lưu'),
				));

		//$Form->isValid($Default);

		return $Form;
	}

	public function addAction() {
		$Form = $this->renderForm('AddNewPost');

		if ($this->getRequest()->isPost()) {
			if ($Form->isValid($this->getRequest()->getPost())) {
				$Post = new Admin_Model_Post_Post();
				$Post->setTitle($Form->getValue('Title'))
						->setContent($Form->getValue('Content'))
						->setCreated(Zend_Date::now()->getTimestamp())
						->setModified($Post->getCreated())
						->setPublished(Zend_Date::now()->setDate($Form->getValue('Published'))->getTimestamp())
						->setStatus($Form->getValue('Status'))
						->setUserId(1);
				$Mapper = new Admin_Model_Post_Mapper('Db');
				$Id = $Mapper->save($Post);

				if ($Id) {
					$this->redirect($this->view->serverUrl() . $this->view->url(array('action' => 'update', 'id' => $Id)));
				}
				
			} else {
				$Messages = '<ul class="message error">';

				foreach (Blog_Message_Form::getInstance()->setForm($Form)->getMessages() as $Message) {
					$Messages .= '<li>' . $Message . '</li>' . PHP_EOL;
				}
				$Messages .= '</ul>';

				$this->view->assign('Message', $Messages);
			}
		}

		$this->view->assign('AddNewPost', $Form);
	}

	public function updateAction() {
		$Mapper = new Admin_Model_Post_Mapper('Db');
		$Post = $Mapper->find((int) $this->getRequest()->getParam('id'));

		$Data = array(
			'Title' => $Post->getTitle(),
			'Content' => $Post->getContent(),
			'Status' => $Post->getStatus(),
			'Published' => Zend_Date::now()->setTimestamp($Post->getPublished())->toString(),
		);

		if ($this->getRequest()->isPost()) {
			$Data = array_merge($Data, $this->getRequest()->getPost());
			$Post->setTitle($Data['Title'])
					->setContent($Data['Content'])
					->setStatus($Data['Status'])
					->setModified(Zend_Date::now()->getTimestamp())
					->setPublished(Zend_Date::now()->set($Data['Published'])->toValue());
		}

		$Form = $this->renderForm('UpdatePost');
		$Form->isValid($Data);

		$this->view->assign('UpdatePost', $Form);
	}

	public function detailAction() {
		$Form = $this->renderForm();
	}

	public function listAction() {
		$Mapper = new Admin_Model_Post_Mapper('Db');
		$Paginator = $Mapper->fetchPagination(null, 'Published DESC');
		$Paginator->setCurrentPageNumber((int) $this->getRequest()->getParam('page'))->setItemCountPerPage(20)->setPageRange(5);
		$From = $Paginator->getItemCountPerPage() * ($Paginator->getCurrentPageNumber() - 1) + 1;

		$this->view->assign('From', $From);
		$this->view->assign('To', min($From + $Paginator->getItemCountPerPage() - 1, $Paginator->getTotalItemCount()));
		$this->view->assign('Total', $Paginator->getTotalItemCount());
		$this->view->assign('Posts', $Paginator->getCurrentItems());
		$this->view->assign('Paginator', (string) $Paginator->render());
	}

}
